﻿using AlarmPlatformInterface.Comm;
using AlarmPlatformInterface.DMs;
using Entity.Models;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Utility;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace AlarmPlatformInterface.Controllers.ConstControllers
{
    [Route("api/Common/Const/[controller]")]
    [ApiController]
    public class GetEventClassController : ControllerBase
    {
        // GET: api/<GetEventClassController>
        [HttpGet]
        public ContentResult Get(ReqBaseModel req)
        {
            return GetEventClass(req);
        }

        // POST api/<GetEventClassController>
        [HttpPost]
        public ContentResult Post([FromBody] ReqBaseModel req)
        {
            return GetEventClass(req);
        }

        private ContentResult GetEventClass(ReqBaseModel req)
        {
            ResBaseModel r = new ResBaseModel();
            try
            {
                string newToken = "";
                OperatorInfo uinfo = new OperatorInfo();
                if (Global.ValidateToken(ref req, ref newToken, ref uinfo))
                {
                    using (Entity.DBContext context = Entity.BaseContext.CreateContext(Global.Configuration["ConnectionStrings:DDb"]))
                    {
                        var v = (from p in context.eventClasses
                                 select p).ToList();
                        r.Result = v.SerializerObjectToString();
                    }
                    r.IsSuccess = true;
                    r.Token = newToken;
                }
                else
                {
                    r.FailType = eFailType.LoginAgain.ToString();
                    r.Result = "登录过期，请重新登录！";
                    return Content(JsonConvert.SerializeObject(r));
                }
            }
            catch (Exception ex)
            {
                WriteLog.WriteExceptionFile("Get Event Class info Controller : " + ex.Message + "\r\n" + ex.StackTrace);
                r.FailType = eFailType.ServerError.ToString();
                r.Result = "服务器错误！";
            }
            return Content(JsonConvert.SerializeObject(r));
        }
    }
}
